<template>
  <div class="register-add">
    <h2 class="page-title">设备登记添加</h2>

    <div class="add">

      <el-form :model="detail" :rules="rules" ref="form" label-width="80px">
        <!-- 设备名称 -->
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="detail.name"></el-input>
        </el-form-item>

        <!-- 设备分类 -->
        <el-form-item label="设备分类" prop="category">
          <el-select v-model="detail.category" >
            <el-option
              v-for="d in $store.state.categoryList"
              :key="d.id"
              :label="d.name"
              :value="d.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <!-- 设备数量 -->
        <el-form-item label="设备数量" prop="number">
          <el-input v-model="detail.number"></el-input>
        </el-form-item>

        <!-- 设备价格 -->
        <el-form-item label="设备价格" prop="price">
          <el-input v-model="detail.price"></el-input>
        </el-form-item>

        <!-- 设备日期 -->
        <el-form-item label="购置日期" prop="time">
          <el-date-picker v-model="detail.time" type="date" placeholder="选择日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div class="btn-list">
        <el-button type="primary" @click="submit">保存</el-button>
        <el-button @click="$router.back()">返回列表</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 动态路径传参，使用 $route.params 取值
      // id: this.$route.params.id,
      detail: {
        name: "",
        category: "",
        number: "",
        price: "",
        time: "",
      },
      rules: {
        name: [
          { required: true, message: '请填写设备名称', trigger: 'blur' }
        ],
        category: [
          { required: true, message: '请填写设备分类', trigger: 'blur' }
        ],
        number: [
          { required: true, message: '请填写设备数量', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            console.log(rule, value, callback);
            if (/^[1-9]\d*$/.test(value)) {
              callback();
            } else {
              callback(new Error('设备数量必须为正整数'));
            }
          },   trigger: 'blur' }
        ],
        price: [
          { required: true, message: '请填写设备价格', trigger: 'blur' }
        ],
        time: [
          { required: true, message: '请填写购置日期', trigger: 'change' }
        ]
      }
    };
  },
  methods: {
    submit() {
      console.log({...this.detail})
      this.$refs['form'].validate(valid => {
        if (!valid) return;

        this.axios.post('/pre-add', {
          ...this.detail
        }).then(data => {
          if (!data.code) {
            this.$message.success('添加成功');
            this.$router.push('/main/register/list');
          }
        })

      })
    }
  }
};
</script>

<style lang="sass" scoped>
.register-add
  padding: 0 30px 30px
  .add
    background: #eee
    border-radius: 5px
    padding: 0 20px 20px
    .el-form
      padding-top: 30px
      width: 300px
      .el-select
        width: 100%
</style>
